package beans;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

@SessionScoped
@ManagedBean
public class UserPageBean {
	public UserPageBean(){}
	private String page="requestResources";

	private static Map<String, Object> countries;
	static {
		countries = new LinkedHashMap<String, Object>();
		countries.put("EN", new Locale("en")); // label, value
		countries.put("RO", new Locale("ro"));
		countries.put("DE", new Locale("de"));
	}
	
	public Map<String, Object> getCountriesInMap() {
		return countries;
	}
	
	// value change event listener
		public void countryLocaleCodeChanged(ValueChangeEvent e) {

			String newLocaleValue = e.getNewValue().toString();

			// loop country map to compare the locale code
			for (Map.Entry<String, Object> entry : countries.entrySet()) {

				if (entry.getValue().toString().equals(newLocaleValue)) {

					FacesContext.getCurrentInstance().getViewRoot()
							.setLocale((Locale) entry.getValue());

				}
				FacesContext.getCurrentInstance().renderResponse();
			}
		}
	
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		System.out.println("Page set to:"+page);
		this.page = page;
	}
	
	

}
